import click
from flask.cli import AppGroup
from app.models import User
from app.extensions import db


def register_cli(app):
    init_cli = AppGroup('init', short_help='初始化工作')

    @init_cli.command(short_help='生成管理员')
    @click.option('--username', default='admin', help='账号')
    @click.option('--password', default='123456', help='密码')
    def admin(username, password):
        # if User.query.filter_by(username=username).first():
        #     raise '账号已经生成，不需要重复生成！'
        for i in range(1, 50):
            user = User(username='{}{}'.format(username, i), password=password, nickname='江先生')
            db.session.add(user)
        db.session.commit()
        click.echo('成功生成账号:{}, 密码:{}'.format(username, password))

    app.cli.add_command(init_cli)
